package com.example.applicationview.view;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.example.applicationview.R;

/**
 * 测试canvas 画各种图形
 */
public class TestCanvas extends View {

    private Paint mPaint;


    public TestCanvas(Context context) {
        super(context);
        init();
    }

    public TestCanvas(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TestCanvas(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);          //抗锯齿
        mPaint.setColor(getResources().getColor(R.color.colorAccent));//画笔颜色
        mPaint.setStyle(Paint.Style.FILL);  //画笔风格
        mPaint.setTextSize(36);             //绘制文字大小，单位px
        mPaint.setStrokeWidth(5);           //画笔粗细
    }

    //重写该方法，在这里绘图
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(getResources().getColor(R.color.yellow));   //设置画布背景颜色
//        canvas.drawCircle(200, 200, 100, mPaint);           //画实心圆
//        canvas.drawRect(0, 0, 200, 100, mPaint);            //画矩形
//        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.image_before), 0, 0, mPaint);
        //left,top点，决定了左上点；right,bottom点决定了右下点
        canvas.drawRect(100,50,400,400,mPaint);

        canvas.drawArc(new RectF(0, 0, 100, 100),0,120,false,mPaint);  //绘制弧形区域
//        canvas.drawRoundRect(new RectF(10,10,210,110),15,15,mPaint); //画圆角矩形
//        canvas.drawOval(new RectF(0,0,200,400),mPaint); //画椭圆
        drawPath(canvas);//画path
//        canvas.drawText("最喜欢看曹神日狗了~",100,100,mPaint);    //绘制文字
        drawPathText(canvas);
        drawClock(canvas);


// 第二种
//        Rect rect = new Rect(100,100,800,400);
//        canvas.drawRect(rect,mPaint);

// 第三种
//        RectF rectF = new RectF(100,100,800,400);
//        canvas.drawRect(rectF,mPaint);

    }

    private void drawPath(Canvas canvas) {
        Path path = new Path();
        path.moveTo(10, 10); //移动到 坐标10,10
        path.lineTo(100, 50);
        path.lineTo(200,40);
        path.lineTo(300, 20);
        path.lineTo(200, 10);
        path.lineTo(100, 70);
        path.lineTo(50, 40);
        path.close();
        canvas.drawPath(path,mPaint);
    }

    private void drawPathText(Canvas canvas) {
        Path path = new Path();
        path.moveTo(50,50);
        path.lineTo(120, 130);
        path.lineTo(200, 300);
        path.close();
        canvas.drawTextOnPath("最喜欢看曹神日狗了~", path, 50, 50, mPaint);    //绘制文字
    }

    protected void drawClock(Canvas canvas) {
        canvas.translate(canvas.getWidth()/2, 200); //将位置移动画纸的坐标点:150,150
        canvas.drawCircle(0, 0, 100, mPaint); //画圆圈

        //旋转画布
        canvas.rotate(210);
        canvas.save();
        //小刻度画笔对象
        Paint tmpPaint = new Paint(mPaint);
        tmpPaint.setStrokeWidth(1);
        float  y=100;
        int count = 60; //总刻度数

        for(int i=0 ; i <count ; i++){
            if(i%5 == 0){
                canvas.drawLine(0f, y, 0, y+12f, mPaint);
                canvas.drawText(String.valueOf(i/5+1), -4f, y+25f, tmpPaint);

            }else{
                canvas.drawLine(0f, y, 0f, y +5f, tmpPaint);
            }
            canvas.rotate(360/count,0f,0f); //旋转画纸
        }

        //绘制指针外圆
        tmpPaint.setColor(Color.GRAY);
        tmpPaint.setStrokeWidth(4);
        canvas.drawCircle(0, 0, 20, tmpPaint);
        //绘制指针外内圆 和 指针
        tmpPaint.setStyle(Paint.Style.FILL);
        tmpPaint.setColor(Color.YELLOW);
        canvas.drawCircle(0, 0, 10, tmpPaint);
        canvas.drawLine(0, 10, 0, -65, mPaint);


    }

}
